package com.lx.sync.core.executor;

import com.lx.sync.core.config.RetryConfig;
import com.lx.sync.core.util.RetryUtil;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.util.List;

/**
 * 基础同步执行
 *
 * @author machen
 * @date 2020/11/5 14:05
 */
@Slf4j
@NoArgsConstructor
public class BaseSyncExecutor implements SyncExecutor {


    private AbstractSyncService abstractSyncService;


    public BaseSyncExecutor(AbstractSyncService abstractSyncService) {
        this.abstractSyncService = abstractSyncService;
    }

    @Override
    public void executor(List dataList) {
        String idxName = abstractSyncService.getIdxName();
        try {
            log.info("开始同步数据，索引:{}  数据量：{}", idxName, dataList.size());


            RetryUtil.retry(() -> {
                try {
                    abstractSyncService.bulkToDb(dataList);
                    // 更新进度
                    return null;
                } catch (Exception e) {

                    throw e;
                }
            }, RetryConfig.defaultConfig());


        } catch (Exception e) {
            log.error("数据同步失败,异常信息:{}", ExceptionUtils.getStackTrace(e));
            throw new RuntimeException(e.getMessage());
        }
    }
}
